import axios from "axios";
import store from "@/store/index";

var Axios = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
});

Axios.interceptors.request.use(
  (config) => {
    // 携带一个token
    config.headers.Authorization = store.state.token;
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

Axios.interceptors.response.use(
  (config) => {
    // 如果status的值大于等于400 全部reject
    if (config.data?.meta?.status >= 400) {
      return Promise.reject(config.data.meta.status);
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default Axios;
